﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyCollection
{
	using System;
	using System.Collections.Generic;

	public class MyStack<T>
	{
		public int Capacity
		{
			get;
			set;
		}

		protected T[] StackArray;

		int StackPointer = 0;

		public MyStack()
		{
			Capacity = 2;
			StackArray = new T[2];
		}

		public MyStack(int capacity)
		{
			Capacity = capacity;
			StackArray = new T[Capacity];
		}

		public int Count
		{
			get { return StackPointer; }
		}

		public void Push(T x)
		{
			if (StackPointer == Capacity)
			{
				IncreaseCapacity();
				StackArray[StackPointer++] = x;
				return;
			}

			if (StackPointer < Capacity)
				StackArray[StackPointer++] = x;
		}

		private void IncreaseCapacity()
		{
			this.Capacity++;
			this.Capacity *= 2;

			MyStack<T> temp = new MyStack<T>(Capacity);

			for (int i = 0; i < this.Count; i++)
			{
				temp.StackArray[i] = StackArray[i];
			}

			this.StackArray = temp.StackArray;
			temp.StackPointer = StackPointer;
		}

		public T Pop()
		{
			return (StackPointer > 0)
						? StackArray[--StackPointer]
						: StackArray[0];
		}

		public void PrintAll()
		{
			for (int i = StackPointer - 1; i >= 0; i--)
				Console.WriteLine(" Value: {0}", StackArray[i]);
		}
	}

}
